﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FWMS
{
    public partial class Form_login : Form
    {
        public Form_login()
        {
            InitializeComponent();
            this.tb_password.Enabled = true;
            this.tb_username.Enabled = true;
            this.bn_cannel.Enabled = true;
            this.bn_ok.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.tb_password.Enabled = true;
            this.tb_username.Enabled = true;
            this.bn_cannel.Enabled = true;
            this.bn_ok.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();

        }
        private void bn_ok_Click(object sender, EventArgs e)
        {
            Bean.UserBean bean = new Bean.UserBean();
            bean.Username = tb_username.Text.Trim();
            bean.Password = tb_password.Text.Trim();
            if ("".Equals(bean.Username.Trim()))
            {
                MessageBox.Show("用户名不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_username.Focus();
                return;
            }
            if ("".Equals(bean.Password.Trim()))
            {
                MessageBox.Show("密码不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_password.Focus();
                return;
            }

            if ((bean.Username.Length > 12) || (bean.Password.Length != 6))
            {
                MessageBox.Show("用户名或密码输入有误,请重新输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_password.Text = "";
                this.tb_username.Focus();
                return;
            }
            Service.UserService userservice = new Service.UserService(); 
            //查询数据库
            bean = userservice.userServiceSelect(bean);
            if (bean.Username != null && bean.Password != null)
            {
                COMMAND.CommandBean.username = bean.Username.Trim();
                COMMAND.CommandBean.usecompany = "大连凯琳食品添加剂有限公司";
                COMMAND.CommandBean.nowtime = DateTime.Now.ToString("yyyy年MM月dd日");
                this.Hide();
                Form_main formmain = new Form_main();
                formmain.Show();
            }
            else
            {
                this.tb_password.Text = "";
                MessageBox.Show("用户名或密码输入有误,请重新输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_username.Focus();
            }
        }

        private void Form_login_Load(object sender, EventArgs e)
        {
            int trialPeriod = (new DateTime(2011, 3, 20)-System.DateTime.Now ).Hours;
            if (trialPeriod<0)
            {
                this.bn_ok.Enabled = false;
                this.tb_username.Enabled = false;
                this.tb_password.Enabled = false;
            }
            this.Opacity = 0;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity += 0.05;
        }

    }
}
